from django.db import models

# Create your models here.
from django.db import models


class Message(models.Model):
    """站内消息"""
    sender    = models.ForeignKey('user.CustomUser', on_delete=models.CASCADE, related_name='sent_messages')
    recipient = models.ForeignKey('user.CustomUser', on_delete=models.CASCADE, related_name='received_messages')
    title     = models.CharField("标题", max_length=120)
    content   = models.TextField("内容")
    unread    = models.BooleanField("未读", default=True)
    created   = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ["-created"]
        verbose_name = "站内消息"

    def mark_read(self):
        self.unread = False
        self.save(update_fields=['unread'])